package cn.edu.zafu.bigdata.mahuilin;

import java.io.*;
import java.util.*;

public class StudentManagementSystem {
    private static final String FILE_NAME = "students.dat";
    private Map<String, Student> students = new HashMap<>();

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        StudentManagementSystem system = new StudentManagementSystem();
        system.loadStudents();

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1:
                    system.addStudent(scanner);
                    break;
                case 2:
                    system.retrieveStudent(scanner);
                    break;
                case 3:
                    system.updateStudent(scanner);
                    break;
                case 4:
                    system.deleteStudent(scanner);
                    break;
                case 5:
                    system.saveStudents();
                    scanner.close();
                    return;
                default:
                    System.out.println("无效的选项，请重新输入。");
            }
        }
    }

    private void loadStudents() throws IOException, ClassNotFoundException {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            students = (Map<String, Student>) ois.readObject();
        } catch (EOFException e) {
            // 文件为空，初始化为空的HashMap
            students = new HashMap<>();
        }
    }

    private void saveStudents() throws IOException {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(students);
        }
    }

    private void addStudent(Scanner scanner) {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (students.containsKey(id)) {
            System.out.println("学号重复，请重新输入。");
            addStudent(scanner);
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();
        Student student = new Student(id, name, phone);
        students.put(id, student);
        System.out.println("学生信息添加成功。");
    }

    private void retrieveStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号（输入QUIT退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = students.get(id);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    private void updateStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号（输入QUIT退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = students.get(id);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                System.out.println("学生信息更新成功。");
            } else {
                System.out.println("未找到该学生");
            }
        }
    }

    private void deleteStudent(Scanner scanner) {
        while (true) {
            System.out.print("请输入学号（输入QUIT退出）: ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            if (students.remove(id) != null) {
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生");
            }
        }
    }
}
